package absc.editor;

import org.eclipse.jface.dialogs.MessageDialog;
import org.eclipse.jface.window.ApplicationWindow;
import org.eclipse.swt.SWT;
import org.eclipse.swt.custom.CLabel;
import org.eclipse.swt.events.SelectionEvent;
import org.eclipse.swt.events.SelectionListener;
import org.eclipse.swt.layout.GridData;
import org.eclipse.swt.layout.GridLayout;
import org.eclipse.swt.widgets.Button;
import org.eclipse.swt.widgets.Composite;
import org.eclipse.swt.widgets.Control;
import org.eclipse.swt.widgets.Shell;
import org.eclipse.swt.widgets.Text;

import absc.object.SWOTObject;
import absc.util.Constantes;

public class SWOTEditorItemPage extends ApplicationWindow implements SelectionListener{
	
	private String title, label, nomeItem;
	private Button btSave, btCancel;
	private int tipoItem;
	private Text txtItem;
	private SWOTObject swot;
	private boolean update;
	private SWOTEditorDesignPage editor;
	
	public SWOTEditorItemPage(Shell parentShell, int tipoItem, SWOTObject swot, String nomeItem, SWOTEditorDesignPage editor) {
		super(parentShell);
		
		if (tipoItem == Constantes.STRENGHT){
			title = "New Strenght";
			label = "Strength:";
		}
		else if (tipoItem == Constantes.WEAKNESSES){
			title = "New Weakness";
			label = "Weakness:";
		}
		if (tipoItem == Constantes.OPPORTUNITIES){
			title = "New Opportunity";
			label = "Opportunity:";
		}
		if (tipoItem == Constantes.THREATS){
			title = "New Threat";
			label = "Threat:";
		}
		
		this.tipoItem = tipoItem;
		this.swot = swot;
		this.nomeItem = nomeItem;
		this.editor = editor;
		
		update = ! nomeItem.equalsIgnoreCase("");
	}
	
	protected Control createContents(Composite parent) {
		Composite tela;
		CLabel lblItem;
		GridLayout layoutTela;
		GridData layoutEdit, layoutSaveButton;		
		
		parent.getShell().setText(this.title);
		
		layoutTela = new GridLayout();
		layoutTela.numColumns = 2;
		
		layoutEdit = new GridData();
		layoutEdit.heightHint = -1;
		layoutEdit.widthHint = 150;		
		
		tela = new Composite(parent, SWT.NONE);
		tela.setLayout(layoutTela);
		tela.setFocus();
		
		lblItem = new CLabel(tela, SWT.NONE);
		lblItem.setText(this.label);
		
		txtItem = new Text(tela, SWT.BORDER);
		txtItem.setLayoutData(layoutEdit);
		txtItem.setText(this.nomeItem);
		txtItem.setFocus();
		
		layoutSaveButton = new GridData();
		layoutSaveButton.horizontalAlignment = org.eclipse.swt.layout.GridData.END;
		
		this.btSave = new Button(tela, SWT.NONE);
		this.btSave.setText("Save");
		this.btSave.setLayoutData(layoutSaveButton);
		this.btSave.addSelectionListener(this);
		
		this.btCancel = new Button(tela, SWT.NONE);
		this.btCancel.setText("Cancel");
		this.btCancel.addSelectionListener(this);
		
		return parent;		
	}

	public void widgetDefaultSelected(SelectionEvent arg0) {
		// TODO Auto-generated method stub
		
	}

	public void widgetSelected(SelectionEvent event) {
		if (event.getSource().equals(this.btCancel)){
			close();
		}
		else if (event.getSource().equals(this.btSave)){
			if (this.txtItem.getText().equals(""))
				MessageDialog.openError(getShell(), "Error", "The item's name cannot be null.");
			else{
				if (update)
					swot.updateItem(this.txtItem.getText(), this.tipoItem, this.nomeItem);
				else
					swot.addItem(this.txtItem.getText(), this.tipoItem);
				
				this.editor.refresh();
				this.close();
			}
		}
	}
}
